package nz.ac.massey.jbuif.editor.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import nz.ac.massey.jbuif.editor.BeanEditor;


/**
 * Class BaseComponent
 * @author Daniel Haselden
 * This is the base class for all editor panels. All editors should extend this panel
 */
public class BaseComponent extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel propertyPanel;
	private JPanel backgroundPanel;
	private String propertyName;
	private String propertyDisplayName;
	private BeanEditor parent;
	private JLabel propertyLabel;
	private Object propertyValue;
	
	
	/**
	 * constructor
	 * @param p bean editor instance 
	 * @param name a name
	 * @param displayName displayed name
	 * @param pev an object
	 */
	public BaseComponent(BeanEditor p,String name,String displayName, Object pev){
		parent = p;
		propertyName = name;
		propertyDisplayName = displayName;
		propertyValue = pev;

		/*Structure of the Panel layout.nesting
		backgroundPanel
			propertyPanel
				propertyLabel	//The property name
				newProperty		//The property editor, ie textField
		*/

		backgroundPanel = new JPanel(new BorderLayout());
		propertyPanel = new JPanel(new GridBagLayout());
		propertyLabel = new JLabel();

		
		//LineBorder lineBorder = (LineBorder)BorderFactory.createLineBorder(Color.black); 
		Border emptyBorder = BorderFactory.createEmptyBorder(5, 10, 5, 10);
		backgroundPanel.setBorder(emptyBorder);
		backgroundPanel.setBackground(Color.white);
		
		//Property label constraints
		propertyLabel.setText(propertyDisplayName);
		GridBagConstraints plc = new GridBagConstraints();
		plc.insets = new Insets(10, 10, 10, 10);
		plc.anchor = GridBagConstraints.WEST;
		
		backgroundPanel.add(propertyPanel);
		propertyPanel.add(propertyLabel,plc);
	}
	
	
	/**
	 * getPanel get the JPanel
	 * @return return a JPanel
	 */
	public JPanel getPanel(){
		return backgroundPanel;
	}
	
	
	/**
	 * getPropertyName get the property name
	 * @return string property
	 */
	public String getPropertyName(){
		return propertyName;
	}
	
	
	/**
	 * setEntryValid set a valid entry
	 * @param v a boolean
	 * Can call this method to change the display test of the editor to red if data is invalid
	 */
	public void setEntryValid(boolean v){
		if(v){
			propertyLabel.setText(propertyDisplayName);
		}else {
			propertyLabel.setText("<html><span style='color:red'>"+propertyDisplayName+ " invalid!</span></html>");
		}
	}
	
	
	/**
	 * addPropertyComponent add a property component
	 * @param newProperty a component
	 */
	public void addPropertyComponent(Component newProperty){
		//All extensions of this class add there component via this method
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(10, 10, 10, 10);
		c.weightx = 0.9;
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.EAST;
		
		propertyPanel.add(newProperty,c);
	}
	
	
	/**
	 * getBeanEditor get the bean editor
	 * @return a bean editor
	 */
	public BeanEditor getBeanEditor(){
		return parent;
	}
	
	
	/**
	 * getPropertyValue get the property value of object
	 * @return object property value
	 */
	public Object getPropertyValue(){
		return propertyValue;
	}
	
	
	/**
	 * setPropertyValue set a property of an object
	 * @param spv an object 
	 */
	public void setPropertyValue(Object spv){
		propertyValue = spv;
	}
}
